﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Spore.Utils
{
    public class ThreadWorkItem : IThreadWorkItem
    {
        private ThreadWorkItemState state;

        /// <summary>
        /// 获取执行状态
        /// </summary>
        public ThreadWorkItemState ExeState
        {
            get { return this.state; }
            internal set
            {
                if (this.state != value)
                {
                    this.state = value;
                }
            }
        }

        /// <summary>
        /// 获取执行结果
        /// </summary>
        public ThreadExeResult ExeResult
        {
            get;
            internal set;
        }

        /// <summary>
        /// 开始执行事件，在线程工作器上被调用
        /// </summary>
        public event Action<IThreadWorkItem> BeginExeWorkEventHandle;

        /// <summary>
        /// 执行结束事件，在线程工作器上被调用
        /// </summary>
        public event Action<ThreadExeResult, IThreadWorkItem> EndExeWorkEventHandle;

        public ThreadWorkItem(Delegate dele, ThreadWorkPriority priority, params object[] workParams)
        {
            this.WorkDele = dele;
            this.Priority = priority;
            this.WorkParams = workParams;
        }

        public ThreadWorkItem(Delegate dele,params object[] workParams):this(dele,ThreadWorkPriority.Common,workParams)
        {
            
        }

        public ThreadWorkItem(Action action, ThreadWorkPriority priority):this(action,priority,null)
        {
 
        }

        public ThreadWorkItem(Action action)
            : this(action, ThreadWorkPriority.Common, null)
        { 
        }

        /// <summary>
        /// 获取或设置被线程工作器执行的委托
        /// </summary>
        public Delegate WorkDele
        {
            get;
            set;
        }

        /// <summary>
        /// 获取或设置被线程工作器执行的参数
        /// </summary>
        public object[] WorkParams
        {
            get;
            set;
        }

        /// <summary>
        /// 获取或设置执行的优先级
        /// </summary>
        public ThreadWorkPriority Priority
        {
            get;
            private set;
        }

        /// <summary>
        /// 获取或设置是否应该取消执行
        /// </summary>
        public bool IsCancel
        {
            get;
            set;
        }

        internal void OnBeginWork()
        {
            if (this.BeginExeWorkEventHandle != null)
            {
                this.BeginExeWorkEventHandle(this);
            }
        }

        internal void OnEndWork(ThreadExeResult result)
        {
            //if(this.Be)
        }

        protected virtual void OnStateChanged()
        {
            if (this.ExeState == ThreadWorkItemState.Exeing)
            {
                this.OnBeginWork();
            }
            else if (this.ExeState == ThreadWorkItemState.EndExe)
            {
                this.EndExeWorkEventHandle(this.ExeResult, this);
            }
        }
    }
}
